package com.xcl.service.impl;

import com.xcl.mapper.UserMapper;
import com.xcl.pojo.User;
import com.xcl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    //加密器
    @Autowired
    BCryptPasswordEncoder passwordEncoder;

    @Override
    public Integer register(String username, String pwd) {

        //密码加密
        String newPwd = passwordEncoder.encode(pwd);

        return userMapper.register(username, newPwd);
    }

    //检查用户名是否存在
    @Override
    public User findUserByName(String userName) {
        return userMapper.findUserByName(userName);
    }

    @Override
    public boolean checkPassword(String rawPwd, String encodePwd) {
        return passwordEncoder.matches(rawPwd, encodePwd);
    }
}
